package com.shujia.day17.tcp2;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.Scanner;

public class Client {
    public static void main(String[] args) throws IOException {

        // 1.创建Socket连接  public Socket(String host, int port)

        Socket socket = new Socket("localhost", 9999);

        // 2.从控制台获取字符串数据
        Scanner scanner = new Scanner(System.in);

        while (true){
            // 发送数据
            OutputStream outputStream = socket.getOutputStream();
            System.out.println("请输入您要发送的信息：");
            if (scanner.hasNext()) {
                String message = scanner.next();
                outputStream.write(message.getBytes());
                outputStream.flush();
            }

            // 接收数据
            InputStream inputStream = socket.getInputStream();
            byte[] bytes = new byte[1024];
            int read = inputStream.read(bytes);
            String receive = new String(bytes, 0, read);
            System.out.println("接收到服务端的回复："+receive);
        }

    }
}
